package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.tgy.admin.Service.impl.CacheDataServiceImpl;
import com.tgy.cache.AuthorInfo;
import com.tgy.constant.RedisKey;
import com.tgy.core.repository.FriendLinkRepository;
import com.tgy.core.service.BlogUserService;
import com.tgy.mo.FriendLinkMO;
import com.tgy.pojo.BlogUser;
import com.tgy.result.GraceJSONResult;
import com.tgy.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/7/21 20:07
 */
@Api(value = "获取缓存数据", tags = {"获取缓存数据"})
@RestController
@RequestMapping("admin/cache")
public class CacheDataController {

    @Resource
    private RedisUtils redis;

    @Resource
    private FriendLinkRepository friendLinkRepository;

    @Resource
    private BlogUserService blogUserService;

    @ApiOperation("获取友情链接")
    @GetMapping("getFriendLinkList")
    public GraceJSONResult getFriendLinkList(){
        String s = redis.get(RedisKey.FRIEND_LINK);
        List<FriendLinkMO> friendLinklist;
        if (StrUtil.isNotBlank(s)){
            friendLinklist = JSONObject.parseArray(s,FriendLinkMO.class);
        }else {
            friendLinklist = friendLinkRepository.getAllByIsDelete(1);
            redis.set(RedisKey.FRIEND_LINK,JSONObject.toJSONString(friendLinklist));
        }
        return GraceJSONResult.ok(friendLinklist);
    }

    @ApiOperation("获取作者信息")
    @GetMapping("getAuthorInfo")
    public GraceJSONResult getAuthorInfo(){
        String s = redis.get(RedisKey.AUTHOR_INFO);
        AuthorInfo authorInfo;
        if (StrUtil.isNotBlank(s)){
            authorInfo = JSONObject.parseObject(s,AuthorInfo.class);
        }else {
            authorInfo = new AuthorInfo();
            BlogUser blogUser = new BlogUser();
            BeanUtil.copyProperties(authorInfo,blogUser);
            CacheDataServiceImpl.initUser(blogUser);
            // 判断数据库是否有
            BlogUser user = blogUserService.queryById(blogUser.getId());
            if (Objects.isNull(user)){
                blogUserService.insertByBo(blogUser);
            }else {
                blogUserService.updateByBo(blogUser);
            }
            redis.set(RedisKey.AUTHOR_INFO,JSONObject.toJSONString(authorInfo));
        }
        authorInfo.setAccount("");
        authorInfo.setPassword("");
        return GraceJSONResult.ok(authorInfo);
    }

    @ApiOperation("修改作者信息")
    @GetMapping("updateAuthorInfo")
    @SaCheckPermission("authorInfo:update")
    public GraceJSONResult updateAuthorInfo(AuthorInfo ao){
        AuthorInfo authorInfo = new AuthorInfo();
        BeanUtil.copyProperties(ao,authorInfo);
        BlogUser blogUser = new BlogUser();
        BeanUtil.copyProperties(authorInfo,blogUser);
        CacheDataServiceImpl.initUser(blogUser);
        blogUserService.updateByBo(blogUser);
        redis.set(RedisKey.AUTHOR_INFO,JSONObject.toJSONString(authorInfo));
        return GraceJSONResult.ok();
    }
}
